/**
 * 
 */
package com.lemote.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.lemote.utils.reflection.ConvertUtils;

/**
 * @author wuhui
 *
 * 下午01:27:29
 */
@Entity
@Table(name="smt_user")
//默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends IdEntity {

	/**
	 * 用户姓名
	 */
	private String realName;
	
	/**
	 * 工卡号
	 */
	private String workCard;
	
	/**
	 * 密码
	 */
	private String password;
	
	/**
	 * 性别
	 */
	private String sex;
	
	/**
	 * 电话
	 */
	private String phone;
	
	/**
	 * 地址
	 */
	private String addr;
	
	/**
	 * 生日
	 */
	private Date birthday;
	
	private Date createDate;
	
	/**
	 * 状态
	 */
	private String status;
	
	/**
	 * 用户所属组
	 */
	private List<Group> groups;
	
	/**
	 * 用户所拥有的角色
	 */
	private List<Role> roles;
	
	/**
	 * 用户单独授于权限
	 */
	private List<Permission> permissions;
	/**
	 * 备用字段
	 */
	private String reserve1;
	
	/**
	 * 备用字段
	 */
	private String reserve2;
	
	/**
	 * 备用字段
	 */
	private Long reserve3;


	public Date getBirthday() {
		return birthday;
	}

	@ManyToMany(fetch = FetchType.LAZY)   
	@JoinTable(name="smt_user_group", joinColumns={@JoinColumn(name="userid")}, inverseJoinColumns={@JoinColumn(name="groupid")})
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Group> getGroups() {
		if (groups==null) {
			return new ArrayList<Group>();
		}
		return groups;
	}

	public String getPassword() {
		return password;
	}

	@ManyToMany(fetch = FetchType.LAZY)   
	@JoinTable(name="smt_user_permission", joinColumns={@JoinColumn(name="userid")}, inverseJoinColumns={@JoinColumn(name="permissionid")})
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Permission> getPermissions() {
		if (permissions==null) {
			return new ArrayList<Permission>();
		}
		return permissions;
	}

	public String getPhone() {
		return phone;
	}

	public String getRealName() {
		return realName;
	}

	public String getReserve1() {
		return reserve1;
	}

	public String getReserve2() {
		return reserve2;
	}

	public Long getReserve3() {
		return reserve3;
	}

	@ManyToMany  
	@JoinTable(name="smt_user_role", joinColumns={@JoinColumn(name="userid")}, inverseJoinColumns={@JoinColumn(name="roleid")})
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy("id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Role> getRoles() {
		if (roles==null) {
			return new ArrayList<Role>();
		}
		return roles;
	}

	public String getSex() {
		return sex;
	}

	public String getStatus() {
		return status;
	}

	public String getWorkCard() {
		return workCard;
	}


	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public void setReserve1(String reserve1) {
		this.reserve1 = reserve1;
	}

	public void setReserve2(String reserve2) {
		this.reserve2 = reserve2;
	}

	public void setReserve3(Long reserve3) {
		this.reserve3 = reserve3;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void setWorkCard(String workCard) {
		this.workCard = workCard;
	}
	
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	/**
	 * @return the addr
	 */
	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}
	
	@Transient
	@SuppressWarnings("unchecked")
	public List<Long> getPermissionIds() {
		return ConvertUtils.convertElementPropertyToList(permissions, "id");
	}
	@Transient
	@SuppressWarnings("unchecked")
	public List<Long> getRoleIds() {
		return ConvertUtils.convertElementPropertyToList(roles, "id");
	}
	@Transient
	@SuppressWarnings("unchecked")
	public List<Long> getGroupIds() {
		return ConvertUtils.convertElementPropertyToList(groups, "id");
	}
}
